package com.wzu.rjgc_ks.controller;

import com.wzu.rjgc_ks.dao.CompanyMapper;
import com.wzu.rjgc_ks.dao.JobMapper;
import com.wzu.rjgc_ks.dao.JobtypeMapper;
import com.wzu.rjgc_ks.dao.StudentMapper;
import com.wzu.rjgc_ks.pojo.Company;
import com.wzu.rjgc_ks.pojo.Job;
import com.wzu.rjgc_ks.pojo.Jobtype;
import com.wzu.rjgc_ks.pojo.Student;
import com.wzu.rjgc_ks.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/com")
public class CompanyAction {
    @Autowired
    private CompanyService companyService;
    @Autowired
    private CompanyMapper companyMapper;
    @RequestMapping("/index")
    public ModelAndView Find_Com_Info(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        //此处假设公司已登录，可以在主界面登录时将ComId保存到Session范围里，下面这句就可以直接删掉
//        request.getSession().setAttribute("ComId", 1001);
       Integer ComId= (Integer) request.getSession().getAttribute("ComId");
//        companyMapper.deleteByPrimaryKey(1001);
       Company company= companyService.find_Com_info(ComId);
        //此处假设公司已登录，可以在主界面登录时将ComName保存到Session范围里，下面这句就可以直接删掉

//        System.out.println(company.toString());
        mv.addObject("company", company);
//        request.getSession().setAttribute("company", company);
        mv.setViewName("Com_Info_Manage");
    return mv;
    }
    //公司信息编辑——获取.................//////
    @RequestMapping(value = "/info/edit/{comId}", method = RequestMethod.GET)
    public ModelAndView Edit_Com_Info(@PathVariable("comId") int comId, HttpServletRequest request) throws Exception {
        ModelAndView mv = new ModelAndView();
        Company company= companyService.find_Com_info(comId);
//        System.out.println(company.toString());
//        request.getSession().setAttribute("company_eidt", company);
        mv.addObject("company_eidt", company);
        mv.setViewName("Com_Info_Manage");
        return mv;
    }
    //公司信息编辑保存......................../////
    @RequestMapping(value = "/info/edit/save", method = RequestMethod.POST)
    public ModelAndView Com_Info_Edit_Save(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        Company company=new Company();
        Integer ComId= (Integer) request.getSession().getAttribute("ComId");
        company.setComId(ComId);
        company.setComName(request.getParameter("ComName"));
        company.setComType(request.getParameter("ComType"));
        company.setComScale(Integer.parseInt(request.getParameter("ComScale")));
        company.setComAddress(request.getParameter("ComAddress"));
        company.setComContact(request.getParameter("ComContact"));
        company.setComPhone(request.getParameter("ComPhone"));
        company.setComEmail(request.getParameter("ComEmail"));
        company.setComProfile(request.getParameter("ComProfile"));
        try {
            int result=companyService.Com_Info_Edit_Save(company);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Company newcompany= companyService.find_Com_info(ComId);
        mv.addObject("company", newcompany);
        mv.setViewName("Com_Info_Manage");
        return mv;

    }
    @RequestMapping(value = "/job/insert",method = RequestMethod.GET)
    public ModelAndView Get_JobType_Info(){
        List<Jobtype> jobtypeList=companyService.find_All_JobType();
        ModelAndView mv = new ModelAndView();
        mv.addObject("jobtypeList", jobtypeList);
        System.out.println(jobtypeList.toString());
        mv.setViewName("Com_JobInfo_Manage");
        return mv;
    }
    //新增职位......................//////
    @RequestMapping(value = "/job/insert",method = RequestMethod.POST)
    public ModelAndView Com_Job_Insert(HttpServletRequest request){
        Job job=new Job();
        job.setJobName(request.getParameter("jobName"));
        job.setJobTypeId(Integer.parseInt(request.getParameter("jobtypeId")));
        String num=request.getParameter("jobNum");
        int a=1;
        if (num!="")
            a=Integer.parseInt(num);
        job.setJobNum(a);
        job.setJobSalary(request.getParameter("jobSalary"));
        job.setJobEdu(request.getParameter("jobEdu"));
        job.setJobExp(request.getParameter("jobExp"));
        job.setJobAddress(request.getParameter("jobAddress"));
        job.setJobDesc(request.getParameter("jobDesc"));
        job.setJobAsk(request.getParameter("jobAsk"));
        job.setJobDuty(request.getParameter("jobDuty"));
        job.setJobWelfare(request.getParameter("jobWelfare"));
        Integer ComId= (Integer) request.getSession().getAttribute("ComId");
        job.setJobComId(ComId);
        int result=companyService.Com_Job_Insert(job);
        ModelAndView mv = new ModelAndView();
//        mv.addObject("jobtypeList", jobtypeList);
//        System.out.println(jobtypeList.toString());
        mv.setViewName("forward:/com/job/viewlist");
        return mv;
    }


    //查询职位类型................../////
    @Autowired
    private JobtypeMapper jobtypeMapper;
    @RequestMapping(value = "/job/view/{jobId}")
    public ModelAndView Com_Job_View(@PathVariable("jobId") int jobId,HttpServletRequest request){
//        Integer jobId=Integer.parseInt(request.getParameter("jobId"));
//        int jobId=1;
//        try{
//             jobId=Integer.parseInt(request.getParameter("jobId"));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        Job job=companyService.Com_Job_Find(jobId);
        Jobtype jobtype=jobtypeMapper.selectByPrimaryKey(job.getJobTypeId());
        job.setJobtype(jobtype);
        ModelAndView mv = new ModelAndView();
        mv.addObject("job", job);
        System.out.println(job.toString());
        mv.setViewName("Com_JobInfo_Manage");
        return mv;
    }
    //编辑职位信息先获取................/////
    @RequestMapping(value = "/job/edit/{jobId}",method = RequestMethod.GET)
    public ModelAndView Com_JobInfo_Get(@PathVariable("jobId") int jobId, HttpServletRequest request) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Jobtype> jobtypeList2=companyService.find_All_JobType();
        mv.addObject("jobtypeList2", jobtypeList2);
        System.out.println(jobId);
        Job job= companyService.Com_Job_Find(jobId);
//        System.out.println(company.toString());
//        request.getSession().setAttribute("company_eidt", company);
        Jobtype jobtype=jobtypeMapper.selectByPrimaryKey(job.getJobTypeId());
        job.setJobtype(jobtype);
        mv.addObject("job_eidt_get", job);
        mv.setViewName("Com_JobInfo_Manage");
        return mv;
    }
    //编辑职位信息——提交...................../////
    @RequestMapping(value = "/job/edit/{jobId}",method = RequestMethod.POST)
    public ModelAndView Com_JobInfo_Edit(HttpServletRequest request,@PathVariable("jobId") int jobId){
        Job job=new Job();
        job.setJobId(jobId);
        job.setJobName(request.getParameter("jobName"));
        job.setJobTypeId(Integer.parseInt(request.getParameter("jobtypeId")));
        String num=request.getParameter("jobNum");
        int a=1;
        try{
            if (num!="")
                a=Integer.parseInt(num);
        } catch (Exception e) {
            a=1;
            e.printStackTrace();
        }

        job.setJobNum(a);
        job.setJobSalary(request.getParameter("jobSalary"));
        job.setJobEdu(request.getParameter("jobEdu"));
        job.setJobExp(request.getParameter("jobExp"));
        job.setJobAddress(request.getParameter("jobAddress"));
        job.setJobDesc(request.getParameter("jobDesc"));
        job.setJobAsk(request.getParameter("jobAsk"));
        job.setJobDuty(request.getParameter("jobDuty"));
        job.setJobWelfare(request.getParameter("jobWelfare"));
        Integer ComId= (Integer) request.getSession().getAttribute("ComId");
        job.setJobComId(ComId);
        int result=companyService.Com_Job_Edit(job);
        System.out.println(job);
        ModelAndView mv = new ModelAndView();
//        mv.addObject("jobtypeList", jobtypeList);
//        System.out.println(jobtypeList.toString());
        mv.setViewName("forward:/com/job/view/"+job.getJobId());
        return mv;
    }


    //查询该公司的职位信息列表.....................//////
    @Autowired
    private JobMapper jobMapper;
    @RequestMapping(value = "/job/viewlist")
    public ModelAndView Com_Job_ViewList(HttpServletRequest request){

        ModelAndView mv = new ModelAndView();
        Integer ComId= (Integer) request.getSession().getAttribute("ComId");
        List<Job> joblist=jobMapper.selecAll_Job_ByComId(ComId);
        mv.addObject("jobViewList", joblist);

//        System.out.println("111111111111111"+joblist.toString());
        mv.setViewName("Com_JobInfo_Manage");
        return mv;
    }
    //删除公司职位信息...........................//
    @RequestMapping(value = "/job/delete/{jobId}")
    public ModelAndView Com_Job_Delete(@PathVariable("jobId") int jobId,HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        int result=jobMapper.deleteByPrimaryKey(jobId);
        mv.setViewName("forward:/com/job/viewlist");
        return mv;

    }
    @Autowired
    private StudentMapper studentMapper;
    //查看简历筛选页面...........................//
    @RequestMapping(value = "/resume/selectsHtml")
    public ModelAndView Stu_Rea_List(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        mv.addObject("ResumeSelectHtml", "11");
        mv.setViewName("Com_JobInfo_Manage");
        return mv;

    }
    //筛选简历...........................//
    @RequestMapping(value = "/resume/selects")
    public ModelAndView Stu_Rea_Selects(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("jobtypeId",Integer.parseInt(request.getParameter("jobtypeId")));
        map.put("grade",request.getParameter("grade"));
        map.put("major",request.getParameter("major"));
        List<Student> stuResumelist=studentMapper.Select_Stu_Resu(map);
        mv.addObject("stuResumelist", stuResumelist);
        System.out.println(stuResumelist.toString());
        mv.setViewName("Com_JobInfo_Manage");
        return mv;

    }
    //查看学生详情简历与求职意向...........................//
    @RequestMapping(value = "/resume/select_view/{stuId}")
    public ModelAndView Stu_Resu_View(@PathVariable("stuId") int stuId,HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        Student stuResumeview=studentMapper.selectStuResumeAndIntent(stuId);
        mv.addObject("stuResumeview", stuResumeview);
        System.out.println(stuResumeview.toString());
        mv.setViewName("Com_JobInfo_Manage");
        return mv;

    }

    //收藏学生简历.................................//
    @RequestMapping(value = "/resume/collect/{reaId}")
    @ResponseBody
    public String Com_Collect_StuResume(@PathVariable("reaId") int reaId,HttpServletRequest request){
        Integer ComId= (Integer) request.getSession().getAttribute("ComId");
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("ComId",ComId);
        map.put("reaId",reaId);
        int stuResumeview=companyMapper.insert_Into_Com_Collect_Rea(map);
        return "收藏成功";

    }

    //查询收藏简历列表.................................//
    @RequestMapping(value = "/resume/collectlist")
    public ModelAndView Com_Collect_ViewList(HttpServletRequest request){

        ModelAndView mv = new ModelAndView();
        Integer ComId= (Integer) request.getSession().getAttribute("ComId");
        List<Student> CollectstuResumelist=studentMapper.Com_Collect_StuResume(ComId);

        mv.addObject("CollectstuResumelist", CollectstuResumelist);

        System.out.println("6666666666666666"+CollectstuResumelist.toString());
        mv.setViewName("Com_JobInfo_Manage");
        return mv;
    }

    //查看收藏的学生详情简历与求职意向...........................//
    @RequestMapping(value = "/resume/Collect_select_view/{stuId}")
    public ModelAndView Com_Collect_Stu_Resu_View(@PathVariable("stuId") int stuId,HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        Student stuResumeview=studentMapper.selectStuResumeAndIntent(stuId);
        mv.addObject("CollectStuResumeview", stuResumeview);
        System.out.println(stuResumeview.toString());
        mv.setViewName("Com_JobInfo_Manage");
        return mv;

    }
    //查看简历被收藏情况...........................//
    @RequestMapping(value = "/resume/Job_Collected_View")
    public ModelAndView Com_Job_Collected(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        Integer ComId= (Integer) request.getSession().getAttribute("ComId");
        List<Job> JobCollectedCount=jobMapper.Com_Job_Collected_Count(ComId);
        mv.addObject("JobCollectedCount", JobCollectedCount);
        System.out.println(JobCollectedCount.toString());
        mv.setViewName("Com_JobInfo_Manage");
        return mv;

    }




}
